
#include "stdafx.h"

#include "midi.h"
#include "ShortMsg.h"
#include "MIDIOutDevice.h"
#include "MIDIInDevice.h"

// Using declaration
using midi::CShortMsg;


//---------------------------------------------------------------------
// CShortMsg implementation
//---------------------------------------------------------------------
// Constructor
CShortMsg::CShortMsg(DWORD Msg, DWORD TimeStamp) : 
m_Msg(Msg)
{
    SetTimeStamp(TimeStamp);
}

void CShortMsg::UnpackShortMsg(DWORD Msg, unsigned char &Command,
							   unsigned char &Channel,
							   unsigned char &DataByte1,
							   unsigned char &DataByte2)
{
	Command = static_cast<unsigned char>(Msg & ~midi::SHORT_MSG_MASK);
	Channel = static_cast<unsigned char>(Msg & midi::SHORT_MSG_MASK);
	DataByte1 = static_cast<unsigned char>
		(Msg >> midi::SHORT_MSG_SHIFT);
	DataByte2 = static_cast<unsigned char>
		(Msg >> midi::SHORT_MSG_SHIFT * 2);
}

// Gets message
const char *CShortMsg::GetMsg() const
{
    return reinterpret_cast<const char *>(&m_Msg);
}


// Gets status byte
unsigned char CShortMsg::GetStatus() const
{
    unsigned char Status;
    unsigned char Dummy;

    UnpackShortMsg(m_Msg, Status, Dummy, Dummy);

    return Status;
}


// Gets MIDI channel
unsigned char CShortMsg::GetChannel() const
{
    unsigned char Channel;
    unsigned char Dummy;

    UnpackShortMsg(m_Msg, Dummy, Channel, Dummy, Dummy);

    return Channel;
}


// Gets command code
unsigned char CShortMsg::GetCommand() const
{
    unsigned char Command;
    unsigned char Dummy;

    UnpackShortMsg(m_Msg, Command, Dummy, Dummy, Dummy);

    return Command;
}


// Gets data byte 1
unsigned char CShortMsg::GetData1() const
{
    unsigned char Data1;
    unsigned char Dummy;

    UnpackShortMsg(m_Msg, Dummy, Dummy, Data1, Dummy);

    return Data1;
}


// Gets data byte 2
unsigned char CShortMsg::GetData2() const
{
    unsigned char Data2;
    unsigned char Dummy;

    UnpackShortMsg(m_Msg, Dummy, Dummy, Dummy, Data2);

    return Data2;
}



// Unpacks short message
void CShortMsg::UnpackShortMsg(DWORD Msg, unsigned char &Status,
                               unsigned char &DataByte1,
                               unsigned char &DataByte2)
{
    Status = static_cast<unsigned char>(Msg);
    DataByte1 = static_cast<unsigned char>
                                   (Msg >> midi::SHORT_MSG_SHIFT);
    DataByte2 = static_cast<unsigned char>
                                   (Msg >> midi::SHORT_MSG_SHIFT * 2);
}



